﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentManagement.Models;
using System.Windows;
using System.Data;

namespace DocumentManagement.ViewsModels
{
   public class CreateDocumentINNViewsModel : ViewsModelBase
    {
       string filename;

        private QSHCEntities1 _entityModels;
        public QSHCEntities1 EntityModels
        {
            get { return _entityModels; }
            set
            {
                if (_entityModels != value)
                {
                    _entityModels = value;
                    OnPropertyChanged("EntityModels");
                }
            }
        }


        private CreateSend _myCreateSend;
        public CreateSend MyCreateSend
        {
            get { return _myCreateSend; }
            set
            {
                if (_myCreateSend != value)
                {
                    _myCreateSend = value;
                    OnPropertyChanged("MyCreateSend");
                 
                }
            }
        }

     

       
        private bool _mySpeed1;
        public bool MySpeed1
        {
            get { return _mySpeed1; }
            set
            {
                if (_mySpeed1 != value)
                {
                    _mySpeed1 = value;
                    OnPropertyChanged("MySpeed1");
                }
            }
        }
        private bool _mySpeed2;
        public bool MySpeed2
        {
            get { return _mySpeed2; }
            set
            {
                if (_mySpeed2 != value)
                {
                    _mySpeed2 = value;
                    OnPropertyChanged("MySpeed2");
                }
            }
        }
        private bool _mySpeed3;
        public bool MySpeed3
        {
            get { return _mySpeed3; }
            set
            {
                if (_mySpeed3 != value)
                {
                    _mySpeed3 = value;
                    OnPropertyChanged("MySpeed3");
                }
            }
        }
        private bool _mySpeed4;
        public bool MySpeed4
        {
            get { return _mySpeed4; }
            set
            {
                if (_mySpeed4 != value)
                {
                    _mySpeed4 = value;
                    OnPropertyChanged("MySpeed4");
                }
            }
        }

        public CreateDocumentINNViewsModel()
        {

        }
        public void SaveToData()
        {
            if (MySpeed1 == false && MySpeed2 == false && MySpeed3 == false && MySpeed4 == false)
            {
                MessageBox.Show("กรุณาเลือกชั้นความเร็วของหนังสือ", "กรอกข้อมูลไม่ครบ");
                return;
            }
            //ชั้นความเร็ว
            if (MySpeed1 == true) MyCreateSend.Speed = 1;
            else if (MySpeed2 == true) MyCreateSend.Speed = 2;
            else if (MySpeed3 == true) MyCreateSend.Speed = 3;
            else if (MySpeed4 == true) MyCreateSend.Speed = 4;

            if (MyCreateSend.EntityState == EntityState.Added || MyCreateSend.EntityState == EntityState.Detached) //detached ดึงข้อมูลมาจากดาต้าเบสแล้วมีการแก้ไข
            {
                MyCreateSend.LogData = CreateLogData(LogAction.Create, MyCreateSend.CreateSendID);
                MyCreateSend.Action = 1;
                MyCreateSend.Type = 2; //status 1 คือการสร้างหนังสือออกภายใน
  
                MyCreateSend.Active = true;
                EntityModels.CreateSends.AddObject(MyCreateSend);
            }
            else if (MyCreateSend.EntityState == EntityState.Detached)// มีการแก้ไขข้อมูล
            {

                MyCreateSend.LogData = CreateLogData(LogAction.Edit, MyCreateSend.CreateSendID);
            }

        
         
            EntityModels.SaveChanges();


            //Clear Data And Autonumber
            //MyCreateSend = new CreateSend { CreateSendDate = DateTime.Now };
            //GetAutoNumber();
        }


        public void GetAutoNumber()
        {

            string year = Convert.ToString(DateTime.Today.Year + 543);

            try
            {

                string AutoNum = EntityModels.CreateSends.OrderByDescending(p => p.CreateSendCode).Where(p => p.Type == 2).First().CreateSendCode;
                //string AutoNum = EntityModels.Sends.OrderByDescending(p => p.SendCode).Where(p => p.Status == 2).First().SendCode;
                var firstcode = AutoNum.Substring(17, 4);
                var lastcode = Convert.ToInt32(AutoNum.Substring(22, 4));

                if (firstcode != year)
                {
                    MyCreateSend.CreateSendCode = string.Format("{0}/{1}.{2}", "ศธ 0514.7.5.1.10", year, "0001");
                }
                else
                {
                    MyCreateSend.CreateSendCode = string.Format("{0}/{1}.{2}", "ศธ 0514.7.5.1.10", year, (lastcode + 1).ToString("D4"));//digital string 4 ตัว เช่น 0003
                }
            }
            catch (Exception ex)
            {
                MyCreateSend.CreateSendCode = string.Format("{0}/{1}.{2}", "ศธ 0514.7.5.1.10", year, "0001");
            }


        }




        public LogData CreateLogData(LogAction logAction, int primaryKeyID)
        {
            LogData logData = new LogData
            {
                Employee = EntityModels.Employees.Where(p => p.EmployeeCode == "IT007").SingleOrDefault(),
                Action = (byte)logAction,
                LogDateTime = DateTime.Now,
                SchemaName = "BackOffice",
                TableName = "CreateSends",
                PrimaryKeyID = primaryKeyID,
            };
            return logData;
        }

        public enum LogAction
        {
            Login = 1,
            Logout,
            OpenPage,
            Create,
            Edit,
            Delete,
        }

    }
}
